import copy
import importlib
import os

from common.read_xmind import ReadXmind
from common.utils import step
from function import query_function, delete_function, no_operation


class Case:
    def __init__(self):
        self.res = []
        self.case_data = []
        # 获取文件名
        project_path = os.getcwd()
        file_names = os.listdir(project_path + "/operation_type")
        for i in file_names:
            if '__' not in i:
                model = 'operation_type.' + i[:-3]
                module = importlib.import_module(model)
                self.case_data.append(module.result())

    def case(self):
        """
        生成用例
        :return:
        """
        # 循环遍历
        for test_case_type in self.case_data:
            for model_case in test_case_type:
                test_case_type_all = model_case['true'] + model_case['false']
                model_num = test_case_type.index(model_case)
                # 判断是否为空
                if len(test_case_type_all) != 0:
                    for i in test_case_type_all:
                        # 循环此类型下每条用例
                        new_step = copy.deepcopy(i['步骤'])
                        for j in self.case_data:
                            # 循环取其他类型里正确的步骤
                            if j != test_case_type:
                                try:
                                    # 处理某个类型没有数据的情况
                                    new_step = step(model_case['step'], new_step, j[model_num]['true'][0]['步骤'])
                                except IndexError:
                                    pass
                        i['步骤'] = new_step
                        self.res.append(i)
        # 单独处理步骤
        for case in self.res:
            result = []
            for i in case['步骤']:
                if case['步骤'].index(i) == len(case['步骤']) - 1:
                    result.append(case["结果"])
                else:
                    expect_the_result = {"点击": "点击成功", "输入": "输入成功", "选择": "选择成功"}
                    if '点击' in i:
                        result.append(expect_the_result['点击'])
                    elif '输入' in i:
                        result.append(expect_the_result['输入'])
                    elif '选择' in i:
                        result.append(expect_the_result['选择'])
                    else:
                        result.append('')
            case["结果"] = result
        return self.res


if __name__ == '__main__':
    import json
    ReadXmind(path=r'C:\Users\admin\Desktop\NAVS.xmind', sheet='模板').processed_data()
    print(json.dumps(Case().case(), ensure_ascii=False))
